<template>
  <div>
    <el-button type="primary" @click="add()">增加</el-button>
    <!-- 驾驶证管理 -->
    <el-table
      :data="DriveCard"
      style="width: 100%"
      :default-sort="{ prop: 'date' }"
    >
     <el-table-column label="序号" type="index" prop="index" sortable>
        <template slot-scope="scope">
          <span>{{ (page.current - 1) * page.size + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
			<el-table-column label="驾驶证号">
			  <template slot-scope="scope">
			    <span style="margin-left: 10px">{{ scope.row.driveCardId }}</span>
			  </template>
			</el-table-column>
      <el-table-column label="身份证号">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.cardId }}</span>
        </template>
      </el-table-column>

     <el-table-column label="国籍" style="width: 10px;">
        <template slot-scope="scope">
          <span style="margin-left: 10px;width: 10px;">{{ scope.row.nationality }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领证日期">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="准驾车型">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.clazz }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效日期至" prop="date" sortable>
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.validPeriod }}</span>
        </template>
      </el-table-column>
	  <el-table-column label="签发部门">
	    <template slot-scope="scope">
	      <span style="margin-left: 10px">{{ scope.row.department }}</span>
	    </template>
	  </el-table-column>
	  <el-table-column label="当前分数">
	    <template slot-scope="scope">
	      <span style="margin-left: 10px">{{ scope.row.score }}</span>
	    </template>
	  </el-table-column>
	  <el-table-column label="驾照状态">
	    <template slot-scope="scope">
	      <span style="margin-left: 10px">{{ scope.row.state }}</span>
	    </template>
	  </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <span style="margin-left: 10px" @click="update(scope.row)">修改</span>
          <span style="margin-left: 10px" @click="backout(scope.row)">注销</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="创建时间" prop="date" sortable>
        <template slot-scope="scope">
          <span>{{ formatTimer(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      :spam="20"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[3, 5, 10, 50]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      style="text-align: center"
    >
    </el-pagination>


<!-- <el-input placeholder="请输入创建时间" v-model="userForm.CreatDate"></el-input> -->
    <!-- 对话框 -->
   <el-dialog :title="title" :visible.sync="isShow" width="30%">

      <el-form ref="DriveCardForm" label-width="80px">
        <br>
        <el-form-item label="驾驶证号" >
          <el-col :span="22">
            <el-input placeholder="请输入驾驶证号" v-model="DriveCardForm.driveCardId"></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="身份证号" >
          <el-col :span="22">
            <el-input placeholder="请输入身份证号" v-model="DriveCardForm.cardId"></el-input>
          </el-col>
        </el-form-item>

       <el-form-item label="国籍" >
          <el-col :span="22">
            <el-input placeholder="请输入国籍内容" v-model="DriveCardForm.nationality" ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="领证日期">
          <el-col :span="22">
            <el-date-picker
                  v-model="DriveCardForm.createDate"
                  type="datetime"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="准驾车型" >
          <el-col :span="22">
            <el-input placeholder="请输入准驾车型内容" v-model="DriveCardForm.clazz"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="有效日期至">
          <el-col :span="22">
            <el-date-picker
                  v-model="DriveCardForm.validPeriod"
                  type="datetime"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="签发部门">
          <el-col :span="22">
            <el-input placeholder="请输入签发部门内容" v-model="DriveCardForm.department"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="当前分数">
          <el-col :span="22">
            <el-input placeholder="请输入当前分数内容" v-model="DriveCardForm.score"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="驾照状态">
          <el-col :span="22">
            <el-input placeholder="请输入驾照状态" v-model="DriveCardForm.state"></el-input>
          </el-col>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="saveInfo">保存</el-button>
      </span>
    </el-dialog>



    <el-dialog :title="title" :visible.sync="isShow1" width="30%">

       <el-form ref="DriveCardForm1" label-width="80px">
         <br>
         <el-form-item label="驾驶证号" >
           <el-col :span="22">
             <el-input placeholder="请输入驾驶证号" v-model="DriveCardForm.driveCardId" readonly></el-input>
           </el-col>
         </el-form-item>

         <el-form-item label="身份证号" >
           <el-col :span="22">
             <el-input placeholder="请输入身份证号" v-model="DriveCardForm.cardId" readonly></el-input>
           </el-col>
         </el-form-item>

        <el-form-item label="国籍" >
           <el-col :span="22">
             <el-input placeholder="请输入国籍内容" v-model="DriveCardForm.nationality" ></el-input>
           </el-col>
         </el-form-item>
         <el-form-item label="领证日期">
           <el-col :span="22">
             <el-date-picker
                   v-model="DriveCardForm.createDate"
                   type="datetime"
                   placeholder="选择日期"
                   value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss">
             </el-date-picker>
           </el-col>
         </el-form-item>
         <el-form-item label="准驾车型" >
           <el-col :span="22">
             <el-input placeholder="请输入准驾车型内容" v-model="DriveCardForm.clazz"></el-input>
           </el-col>
         </el-form-item>
         <el-form-item label="有效日期至">
           <el-col :span="22">
             <el-date-picker
                   v-model="DriveCardForm.validPeriod"
                   type="datetime"
                   placeholder="选择日期"
                   value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss">
             </el-date-picker>
           </el-col>
         </el-form-item>
         <el-form-item label="签发部门">
           <el-col :span="22">
             <el-input placeholder="请输入签发部门内容" v-model="DriveCardForm.department"></el-input>
           </el-col>
         </el-form-item>
         <el-form-item label="当前分数">
           <el-col :span="22">
             <el-input placeholder="请输入当前分数内容" v-model="DriveCardForm.score"></el-input>
           </el-col>
         </el-form-item>
         <el-form-item label="驾照状态">
           <el-col :span="22">
             <el-input placeholder="请输入驾照状态" v-model="DriveCardForm.state"></el-input>
           </el-col>
         </el-form-item>

       </el-form>

       <span slot="footer" class="dialog-footer">
         <el-button @click="isShow1 = false">取 消</el-button>
         <el-button type="primary" @click="saveInfo">保存</el-button>
       </span>
     </el-dialog>
  </div>
</template>
<script>
import Url from "@/util/URLUtil";
export default {
  name: "AllDriverCard",
  data() {
    return {
      DriveCard: [],
      title:"新增",
      page: {
        current: 1, //当前页
        size: 5, //每页数据条数
        pages: 0, //总页数
        total: 0, //总数据条数
      },
      DriveCardForm:{
        id:null,
        driveCardId:"",
        cardId:"",
        nationality:"",
        createDate:"",
        clazz:"",
        validPeriod:"",
        department:"",
        score:"",
        state:""
      },
      DriveCardForm1:{
        id:null,
        driveCardId:"",
        cardId:"",
        nationality:"",
        createDate:"",
        clazz:"",
        validPeriod:"",
        department:"",
        score:"",
        state:""
      },
      isShow: false,
      isShow1: false
    };
  },
  methods: {
    //初始化表格数据
    initAllDriverTable() {
      var self = this;
      $.ajax({
        url: Url.url + "/api/getAllDriveCard",
        type: "get",
        contentType: "application/json",
        // dataType: "json",
        data: { current: self.page.current, size: self.page.size },
        success(response) {
          // console.log(response);
          self.DriveCard = response.records;
          self.page.total = response.total;
          self.page.size = response.size;
          self.page.current = response.current;
          self.page.pages = response.pages;
        },
      });
    },
    add(){
      //更改模态框的标题
      this.title="新增";
      //显示模态框
      this.isShow=true;
      //清除表单元素
      this.DriveCardForm.driveCardId="";
      this.DriveCardForm.cardId="";
      this.DriveCardForm.nationality="";
      this.DriveCardForm.createDate="";
      this.DriveCardForm.clazz="";
      this.DriveCardForm.validPeriod="";
      this.DriveCardForm.department="";
      this.DriveCardForm.score="";
      this.DriveCardForm.state="";

    },
    //修改驾驶证
    update(row){
       //更改模态框的标题
       this.title="修改";
       //显示模态框
       this.isShow1=true;
       //清除表单元素
       this.DriveCardForm1.driveCardId=row.driveCardId;
       this.DriveCardForm1.cardId=row.cardId;
       this.DriveCardForm1.nationality=row.nationality;
       this.DriveCardForm1.createDate=row.createDate;
       this.DriveCardForm1.clazz=row.clazz;
       this.DriveCardForm1.validPeriod=row.validPeriod;
       this.DriveCardForm1.department=row.department;
       this.DriveCardForm1.score=row.score;
       this.DriveCardForm1.state=row.state;
    },
    //撤销驾驶证
    backout(row){
       var mythis = this;
       console.log(row)
        this.$confirm("你确定要删除数据吗?").then(function() {
          $.ajax({
            url: Url.url + "/aqi/deleteDriverCard/"+row.cardId,
            type: "delete",
            success: function(data) {
              console.log(data.info)
              mythis.$message(data.info)
              if (data.info == "删除成功") {
                //刷新
                mythis.initAllDriverTable();
              }
            }
          })
        });

    },
    saveInfo(){
      var mythis = this;
       var url = "";
       var type="";
        if (this.title == "新增") {
          url = Url.url + "/aqi/driveCard";
          type="post";
        } else {
          url =  Url.url + "/aqi/updateDriverCard";
          type="put";
        }
        console.log(mythis.DriveCardForm);
        $.ajax({
          url: url,
          type: type,
          contentType: "application/json",
          data:JSON.stringify(mythis.DriveCardForm),
          success: function(data) {
            console.log(data)
            mythis.$message(data.info)
            if (data.info == "添加成功"||data.info=="修改成功") {
              //关闭模态框
              mythis.isShow = false;
              //刷新
              mythis.initAllDriverTable();
            }
          }
        })
    },
    //修改每页尺寸
    handleSizeChange(val) {
      this.page.size = val;
      this.page.current = 1;
      this.initAllDriverTable();
    },
    //修改当前页
    handleCurrentChange(val) {
      this.page.current = val;
      this.initAllDriverTable();
    },
    //日期格式化工具
    formatTimer(value) {
      let date = new Date(value);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let m = date.getMinutes();
      m = m < 10 ? "0" + m : m;
      let s = date.getSeconds();
      s = s < 10 ? "0" + s : s;
      return y + "-" + MM + "-" + d + " " + h + ":" + m;
    },
  },
  mounted() {
    this.initAllDriverTable();
  },
};
</script>
